package com.jmu.controller;

import com.jmu.pojo.Student;
import com.jmu.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class StuController {
    @Autowired
    @Qualifier("StuServiceImpl")
    private StuService stuService;

    @RequestMapping("/stu/select")
    @ResponseBody
    public Map<String,Object> select(@RequestParam(value = "name",required = false) String name){
        System.out.println("name" + name);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("code","0");
        map.put("data",stuService.selectStuByName(name));
        System.out.println(map);
        return map;
    }
    @RequestMapping("/stu/del/{UUID}")
    @ResponseBody
    public Map<String,Object> del(@PathVariable(value = "UUID") String uuid){
        Map<String,Object> map = new HashMap<String, Object>();
        if(stuService.delStu(uuid)>0){
            map.put("status","1");
        }else{
            map.put("status","0");
        }
        return map;
    }
    @RequestMapping("/stu/add")
    @ResponseBody
    public Map<String,Object> add(@RequestBody Student student){
        System.out.println("进入添加");
        Map<String,Object> map = new HashMap<String, Object>();
        student.setUUID(UUID.randomUUID().toString().replace("-",""));
        System.out.println(student);
        if(stuService.addStu(student)>0){
            map.put("status","1");
        }else{
            map.put("status","0");
        }
        return map;
    }
    @RequestMapping("/stu/update")
    @ResponseBody
    public Map<String,Object> update(@RequestBody Student student){
        System.out.println(student);
        Map<String,Object> map = new HashMap<String, Object>();
        System.out.println(student);
        if(stuService.updateStu(student)>0){
            map.put("status","1");
        }else{
            map.put("status","0");
        }
        return map;
    }
}
